package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R addCategory(@RequestBody Category category){
        categoryService.save(category);

        return R.success("添加成功！");
    }


    @GetMapping("page")
    public  R page(Integer page,Integer pageSize){
        Page page1=new Page(page,pageSize);
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);
        categoryService.page(page1,wrapper);
        return R.success(page1);

    }
    @DeleteMapping("/{id}")
    public R deleteCategory(@PathVariable Long id){

        return categoryService.deleteCategory(id);
    }

    @PutMapping
    public R updateCategory(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        return R.success("修改成功！");

    }

@GetMapping("/list")
    public R list(Integer type){
        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
    List<Category> list = categoryService.list(wrapper);
    return R.success(list);

}

//    @GetMapping("/list")
//    public R list1(){
////        LambdaQueryWrapper<Category> wrapper=new LambdaQueryWrapper<>();
////        wrapper.eq(Category::getType,type);
//        List<Category> list = categoryService.list();
//        return R.success(list);
//
//    }
}
